package com.mcxx.modules.system.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.system.service.AreaMappingService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.system.dto.AreaMappingDTO;
import com.mcxx.modules.system.param.AreaMappingParam;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@Api("数据管理/行政区划映射")
@RestService({"dataManage/AreaMapping/v2"})
public class AreaMappingController extends DefaultRestService {
    @Autowired
    private AreaMappingService areaMappingService;
    @Autowired
    private AreaReadService areaReadService;













    @GetMapping("queryAreaMappingList")
    @ApiOperation("查询行政区划映射列表")
    public void queryAreaMappingList(AreaMappingParam param){
        log.info("查询行政区划映射列表，参数是：{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        List<Map<String, Object>>  list = areaReadService.queryAreaMappingList(param,userAccountVo);
        setData(list);
        log.info("查询行政区划映射列表，参数是：{}", param);
    }


    @GetMapping("getAreaMappingInfo")
    @ApiOperation("根据民政区划编码查询行政区划映射列表")
    public void getAreaMappingInfo(AreaMappingParam param){
        log.info("根据民政区划编码查询行政区划映射列表，参数是：{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        List<AreaMappingDTO> areaMappingLists = areaReadService.queryAreaMappingInfo(param,userAccountVo);
        setData(areaMappingLists);
        log.info("根据民政区划编码查询行政区划映射列表，参数是：{}", param);
    }


    @GetMapping("getFinanceInfo")
    @ApiOperation("根据民政区划编码查询财政区划信息")
    public void getFinanceInfo(AreaMappingParam param){
        log.info("根据民政区划编码查询财政区划信息，参数是：{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        List<AreaMappingDTO> areaMappingLists = areaReadService.getFinanceInfo(param,userAccountVo);
        setData(areaMappingLists);
        log.info("根据民政区划编码查询财政区划信息，参数是：{}", param);
    }


    @PostMapping("saveMappingInfo")
    @ApiOperation("保存行政区划映射信息")
    public void saveMappingInfo(@RequestBody @V("notnull") AreaMappingParam areaMappingParam) {
        log.info("保存行政区划映射信息，参数是：{}", areaMappingParam);
        areaMappingService.saveMappingInfo(areaMappingParam, getCurUserAccount());
        setData(true);
        log.info("保存行政区划映射信息，结果是：{}", true);
    }


    @PostMapping("downLoadAreaMappingList")
    @ApiOperation("行政区划映射导出")
    public void downLoadAreaMappingList(HttpServletResponse response, @RequestBody AreaMappingParam param)throws IOException {
        log.info("行政区划映射导出开始 参数{}", JSON.toJSONString(param));
        ExcelInfo excelInfo = areaMappingService.downLoadAreaMappingList(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        log.info("行政区划映射导出结束 参数{}", excelInfo);
    }

}
